﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ZhimeiSoft.Framework.Common.Enums;
using ZhimeiSoft.Framework.Client.Context;
using ZhimeiSoft.Framework.Client.Configuration;
using ZhimeiSoft.Framework.Client.Connector.Manager;
using ZhimeiSoft.Framework.Common.Entity;

namespace ZhimeiSoft.Framework.Client.Base
{
    public abstract class ClientAppBase
    {
        public ClientAppBase()
        {
            
        }

        private static ClientAppBase globalClientApp = null;
        private AccessModeEnum accessMode;
        private string serviceUrl;
        private int userId;
        private string username;
        private string password;

        public ClientAppBase GlobalClientApp
        {
            get { return globalClientApp; }
        }

        /// <summary>
        /// 连接方式
        /// </summary>
        public AccessModeEnum AccessMode
        {
            get { return accessMode; }
            set { accessMode = value; }
        }

        /// <summary>
        /// 服务地址
        /// </summary>
        public string ServiceUrl
        {
            get { return this.serviceUrl; }
            set { this.serviceUrl = value; }
        }

        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserId
        {
            get { return this.userId; }
            set { this.userId = value; }
        }

        public string UserName
        {
            get { return this.username; }
            set { this.username = value; }
        }

        /// <summary>
        /// 用户登录密码
        /// </summary>
        public string Password
        {
            get { return this.password; }
            set { this.password = value; }
        }

        public abstract ILoginForm CreateLoginForm();

        public abstract IMainForm CreateMainForm();

        /// <summary>
        /// 创建关于窗体
        /// </summary>
        /// <returns>关于窗体接口</returns>
        public virtual IAboutForm CreateAboutForm()
        {
            return null;
        }

        /// <summary>
        /// 创建修改密码窗体
        /// </summary>
        /// <returns>修改密码窗体接口</returns>
        public virtual IChangePasswordForm CreateChangePasswordForm()
        {
            return null;
        }

        /// <summary>
        /// 初始化配置信息
        /// </summary>
        public virtual void Init()
        {
            //初始化配置信息
            ClientConfiguration clientConfig = new ClientConfiguration();
            string strServiceUrl = clientConfig.ServiceUrl;
            this.serviceUrl = strServiceUrl;
            string strAccessMode = clientConfig.AccessMode;
            if (strAccessMode.ToLower() == "local")
            {
                this.accessMode = AccessModeEnum.emLocal;
            }
            else if (strAccessMode.ToLower() == "webservice")
            {
                this.accessMode = AccessModeEnum.emWebService;
            }
            else
            {
                this.accessMode = AccessModeEnum.emNone;
            }
            int num = (int)this.accessMode;
            ClientContext.Properties["ACCESSMODE"] = num.ToString();
            ClientContext.Properties["SERVICEURL"] = strServiceUrl;
        }

        protected void InitConnector()
        {
            lock (this)
            {
                try
                {
                    ThreadStart starter = new ThreadStart(
                    delegate
                    {
                        ConnectorManager.CreateConnector(this.accessMode, this.serviceUrl);
                    });
                    Thread thread = new Thread(starter);
                    thread.Start();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 运行系统
        /// </summary>
        /// <returns></returns>
        public virtual bool Run()
        {
            this.Init();
            try
            {
                this.InitConnector();

                /*ILoginForm loginForm = this.CreateLoginForm();
                LoginEntity output = loginForm.Login(null);
                if (output == null)
                {
                    return false;
                }

                this.userId = output.UserId;
                this.username = output.UserName;*/
                IMainForm mainForm = this.CreateMainForm();
                mainForm.Execute(this);
                return true;
            }
            finally
            {
            }

        }
    }
}
